package com.hc.service;

import com.hc.bean.CategoryBean;
import com.hc.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<CategoryBean> queryData(CategoryBean categoryBean) {
        return categoryMapper.queryData(categoryBean);
    }

    @Override
    public List<CategoryBean> queryDataByTree(Integer category_id) {
        return categoryMapper.queryDataByTree(category_id);
    }

    @Override
    public int saveData(CategoryBean categoryBean) {
        return categoryMapper.insertData(categoryBean);
    }

    @Override
    public int deleteDataById(Integer category_id) {
      //  System.out.println("调用了categoryserviceimpl中的deletById方法，方法参数为。。。。");
        //根据方法形，先找parentId=方法形参的分类数据，即用于判断要删除的类别是否存在子类别
        List<CategoryBean> categoryBeans  =  categoryMapper.queryDataByParentId(category_id);
        if (categoryBeans.size()>0){
            throw new RuntimeException("该类别下有子类别，不允许删除");
        }else{
            return categoryMapper.deleteDataById(category_id); //0 1
        }



    }

    @Override
    public int updateData(CategoryBean categoryBean) {

      //  System.out.println("调用了categoryserviceimpl中的deletById方法，方法参数为。。。。");
        return categoryMapper.updateData(categoryBean);
    }
}
